home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / rlogin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.4 KB  |  184 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "session.h"
  6. #include "proc.h"
  7. #include "tty.h"
  8. #include "commands.h"
  9. #include "netuser.h"
  10.  
  11. static char *username = "guest";
  12. static char terminal[] = "ansi";
  13. static char *termspeed = "/38400";
  14.  
  15. extern char *Rloguser;
  16. /*extern FILE *Rawterm;*/
  17. int rlo_connect __ARGS((struct session *sp,char *fsocket,int len));
  18. void rlo_output __ARGS((int unused,void *p1,void *p2));
  19. void rlrecv __ARGS((struct session *sp));
  20.  
  21. /* Execute user rlogin command */
  22. int
  23. dorlogin(argc,argv,p)
  24. int argc;
  25. char *argv[];
  26. void *p;
  27. {
  28.     struct session *sp;
  29.     struct sockaddr_in fsocket;
  30.     struct sockaddr_in lsocket;
  31.  
  32.     /* Allocate a session descriptor */
  33.     if((sp = newsession(argv[1],RLOGIN,0,1)) == NULLSESSION){
  34.         tputs(Nosess);        /* Too Many Sessions */
  35.         return 1;
  36.     }
  37.     Current->flowmode = 0;
  38.     fsocket.sin_family = AF_INET;
  39.     if(argc < 3)
  40.         fsocket.sin_port = IPPORT_RLOGIN;
  41.     else
  42.         fsocket.sin_port = atoi(argv[2]);
  43.  
  44.     tprintf("Resolving %s... ",sp->name);
  45.     if((fsocket.sin_addr.s_addr = resolve(sp->name)) == 0L){
  46.         tprintf(Badhost,sp->name);
  47.         keywait(NULLCHAR,1);
  48.         freesession(sp);
  49.         return 1;
  50.     }
  51.     if((sp->s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  52.         tprintf("Can't create socket\n");
  53.         keywait(NULLCHAR,1);
  54.         freesession(sp);
  55.         return 1;
  56.     }
  57.     lsocket.sin_family = AF_INET;
  58.     lsocket.sin_addr.s_addr = INADDR_ANY;
  59.     lsocket.sin_port = IPPORT_RLOGIN;
  60.     bind(sp->s,(char *)&lsocket,sizeof(lsocket));
  61.     return rlo_connect(sp,(char *)&fsocket,SOCKSIZE);
  62. }
  63. /* Generic interactive connect routine */
  64. int
  65. rlo_connect(sp,fsocket,len)
  66. struct session *sp;
  67. char *fsocket;
  68. int len;
  69. {
  70.     unsigned int index;
  71.  
  72.     index = sp - Sessions;
  73.  
  74.     sockmode(sp->s,SOCK_ASCII);
  75.     tprintf("Trying %s...\n",psocket((struct sockaddr *)fsocket));
  76.     if(connect(sp->s,fsocket,len) == -1){
  77.           tprintf("%s session %u failed: %s errno %d\n",
  78.          Sestypes[sp->type], index, sockerr(sp->s),errno);
  79.  
  80.         keywait(NULLCHAR,1);
  81.         freesession(sp);
  82.         return 1;
  83.     }
  84.     tprintf("%s session ",Sestypes[sp->type]);
  85.     tprintf("%u connected to %s\n",index,sp->name);
  86.     log(sp->s,"%4.4s connect",Sestypes[sp->type]);
  87.     rlrecv(sp);
  88.     return 0;
  89. }
  90.  
  91. /* Rlogin input routine, common to both rlogin and ttylink */
  92. void
  93. rlrecv(sp)
  94. struct session *sp;
  95. {
  96.     int c,s,index;
  97.     char *cp;
  98.  
  99.     s = sp->s;
  100.  
  101.     /* We run both the network and local sockets in transparent mode
  102.      * because we have to do our own eol mapping
  103.      */
  104.     seteol(s,"");
  105.     seteol(Curproc->input,"");
  106.     seteol(Curproc->output,"");
  107.  
  108.     /* Read real keystrokes from the keyboard */
  109.     sp->ttystate.crnl = 0;
  110.     /* Put tty into raw mode */
  111.     sp->ttystate.echo = 0;
  112.     sp->ttystate.edit = 0;
  113.  
  114.     setflush(s,'\n');
  115.  
  116.     index = sp - Sessions;
  117.  
  118.     /* Fork off the transmit process */
  119.     sp->proc1 = newproc("rlo_out",1024,rlo_output,0,sp,NULL,0);
  120.  
  121.     /* Process input on the connection */
  122.     while((c = recvchar(s)) != -1){
  123.         putc((char)c,stdout);
  124.     }
  125. quit:    /* A close was received from the remote host.
  126.      * Notify the user, kill the output task and wait for a response
  127.      * from the user before freeing the session.
  128.      */
  129.     cp = sockerr(s);
  130.     seteol(s,"\r\n");
  131.     seteol(Curproc->input,"\r\n");
  132.     seteol(Curproc->output,"\r\n");
  133.  
  134.     tprintf("%s session %u", Sestypes[sp->type],index);
  135.     tprintf(" closed: %s\n", cp != NULLCHAR ? cp : "EOF");
  136.     killproc(sp->proc1);
  137.     sp->proc1 = NULLPROC;
  138.     close_s(sp->s);
  139.     sp->s = -1;
  140.     keywait(NULLCHAR,1);
  141.     freesession(sp);
  142. }
  143.  
  144. /* User rlogin output task, started by user rlogin command */
  145. void
  146. rlo_output(unused,sp1,p)
  147. int unused;
  148. void *sp1;
  149. void *p;
  150. {
  151.     struct session *sp;
  152.     struct mbuf *bp;
  153.     char *cp;
  154.     char *logname;
  155.     sp = (struct session *)sp1;
  156.  
  157.     logname = Rloguser;
  158.     if(logname == NULLCHAR)
  159.         logname = username;
  160.     bp = ambufw(1 + strlen(logname)+1 + strlen(logname)+1 +
  161.         strlen(terminal) + strlen(termspeed)+1);
  162.  
  163.     cp = bp->data;
  164.     *cp++ = '\0';
  165.     strcpy(cp, logname);
  166.     cp += strlen(logname) + 1;
  167.     strcpy(cp, logname);
  168.     cp += strlen(logname) + 1;
  169.     strcpy(cp, terminal);
  170.     cp += strlen(terminal);
  171.     strcpy(cp, termspeed);
  172.     cp += strlen(termspeed) + 1;
  173.     bp->cnt = cp - bp->data;
  174.     if(send_mbuf(sp->s,bp,0,NULLCHAR,0) != -1){
  175.         /* Send whatever's typed on the terminal */
  176.         while(recv_mbuf(sp->input,&bp,0,NULLCHAR,0) > 0){
  177.             if(send_mbuf(sp->s,bp,0,NULLCHAR,0) == -1)
  178.             break;
  179.         }
  180.     }
  181.     /* Make sure our parent doesn't try to kill us after we exit */
  182.     sp->proc1 = NULLPROC;
  183. }
  184.